from playwright.sync_api import sync_playwright
import json


def login():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://team.pescms.com/?g=Team&m=Login&a=index&back_url=Lw==")

        page.wait_for_load_state("networkidle")  # 等待页面完全加载
        # === 1. 获取完整 HTML 内容（DOM 树）===
        page_content = page.content()
        page_evaluate = page.evaluate("document.documentElement.outerHTML")
        # save_dom(page_evaluate=data, page_content=html_content)
        # print(html_content)
        with open(f"page.json", "w", encoding="utf-8") as f:
            json.dump(page_evaluate, f, ensure_ascii=False, indent=2)
        print(f"✅ JSON 已保存到: page.json")

        with open(f"page.html", "w", encoding="utf-8") as f:
            f.write(page_content)
        print(f"✅ HTML 已保存到: page.html")
        print(page_evaluate)
        # browser.close()
        page.locator("input[name='account']").fill('123')
        page.locator("input[name='passwd']").fill('321')
        page.locator("button.am-btn.am-radius.am-btn-primary.am-btn-block").click()
        context.storage_state(path="auth_state.json")
        page.wait_for_timeout(1000)


if __name__ == '__main__':
    login()
